//
//  RightAreaView.m
//  zjGovernment
//
//  Created by 胡胜冬 on 2017/2/18.
//  Copyright © 2017年 胡胜冬. All rights reserved.
//

#import "RightAreaView.h"
#import "AreaAddressCell.h"

@interface RightAreaView ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation RightAreaView

- (void)setAreaArray:(NSArray *)areaArray {
    _areaArray = areaArray;
    if (!_areaArray) {
        _areaArray = [NSArray array];
    }
    [self.tableView reloadData];
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    [self.tableView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _areaArray = [NSArray array];
        [self initTableView];
    }
    return self;
}

- (void)initTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style: UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"f5f8f7"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"AreaAddressCell" bundle:nil] forCellReuseIdentifier:@"AreaAddressCell"];
    
    [self addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.areaArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AreaAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AreaAddressCell" forIndexPath:indexPath];
    
    NSDictionary *dataDic = self.areaArray[indexPath.row];
    
    cell.titleString = dataDic[@"text"];
    
    if (self.indexPath && (indexPath.row == self.indexPath.row)) {
        cell.titleColor = [UIColor colorWithHexString:@"1492ff"];
        
    }else {
        cell.titleColor = [UIColor colorWithHexString:@"333333"];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0 * APP_PARAMETER;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.indexPath = indexPath;
    
    NSDictionary *dataDic = self.areaArray[indexPath.row];
    NSString *titleString = dataDic[@"text"];
    NSString *idString = dataDic[@"id"];
    NSString *codeString = dataDic[@"code"];
    
    if ([self.delegate respondsToSelector:@selector(clickRightAreaViewCellWithTitle:ID:Code:)]) {
        [self.delegate clickRightAreaViewCellWithTitle:titleString ID:idString Code:codeString];
    }
}

@end
